﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI;

namespace Thksoft.UserCotrols
{
    public partial class BinsinessDo : BaseControl
    {
        private Lc_ConditionBLL conBll = new Lc_ConditionBLL();

        private Lc_TransactionBLL tranBll = new Lc_TransactionBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string businessId = Request.QueryString["BusinessId"].ToString();

                string trid = Request.QueryString["trancId"].ToString();

                HandleInformation();

                Lc_Transaction model = new Lc_TransactionBLL().GetInfoById(trid);

                bindGetCirculationConditon(model.N_PROCESS_ID.ToString(), model.N_QUEUE_ID.ToString());

                BindHt();
                Bindyj();
            }
        }

        /// <summary>
        /// 绑定办理信息
        /// </summary>
        private void HandleInformation()
        {
            string trid = Request.QueryString["trancId"].ToString();

            lblWorkUser.Text = lognUser.USER_NAME;

            #region 绑定当前节点信息

            DataTable CurrentNodeData = conBll.GetCurrentNode(trid);
            if (CurrentNodeData != null && CurrentNodeData.Rows.Count > 0)
            {
                Label2.Text = CurrentNodeData.Rows[0]["业务编号"].ToString();

                Label3.Text = CurrentNodeData.Rows[0]["业务名称"].ToString();

                Lc_Queue qModel = new Lc_QueueBLL().GetInfoById(CurrentNodeData.Rows[0]["队列编号"].ToString());

                if (qModel.N_CANCEL == 0)
                {
                    //不允许注销
                    btnzx.Visible = false;
                }

                if (qModel.N_CHANGE == 0)
                {
                    //不允许特殊流转
                    radbllx.Enabled = false;
                }

                Session["TransactionIdBus"] = Convert.ToDecimal(CurrentNodeData.Rows[0]["办理流水号"].ToString() == "" ? "0" : CurrentNodeData.Rows[0]["办理流水号"].ToString());
                Session["BusinessNameBus"] = CurrentNodeData.Rows[0]["业务名称"].ToString();
                Session["BusinessCzyIdBus"] = CurrentNodeData.Rows[0]["业务操作员"].ToString();

                Session["BeginTimeBus"] = CurrentNodeData.Rows[0]["开始时间"].ToString();
                Session["HandleDayNumBus"] = Convert.ToInt32(CurrentNodeData.Rows[0]["办理天数"].ToString().Trim() == "" ? "0" : CurrentNodeData.Rows[0]["办理天数"].ToString().Trim());

                Session["processIdBus"] = CurrentNodeData.Rows[0]["流程编号"].ToString();
                Session["QueueIdBus"] = CurrentNodeData.Rows[0]["队列编号"].ToString();
                Session["QueneNumber"] = CurrentNodeData.Rows[0]["队列序号"].ToString();

                Session["OnTransactionIdBus"] = Convert.ToDecimal(CurrentNodeData.Rows[0]["上级流水"].ToString());
                Session["Audit_TypeBus"] = Convert.ToInt32(CurrentNodeData.Rows[0]["审核方式"].ToString());

                Session["MUT_STATEBus"] = Convert.ToInt32(CurrentNodeData.Rows[0]["节点执行状态"].ToString());
                Session["MEETINGBus"] = Convert.ToInt32(CurrentNodeData.Rows[0]["汇合点"].ToString());
                Session["time_dueBus"] = Convert.ToDateTime(CurrentNodeData.Rows[0]["到期时间"].ToString()); //到期时间
                //  Session["processNameBus"] = pdao.GetModel(Session["processIdBus"].ToString()).PROCESS_NAME;//流程名称
                lblCurrentNode.Text = CurrentNodeData.Rows[0]["节点名称"].ToString();

                #region 特殊情况，下一步审核人

                tr3.Style["Display"] = "None";//多人下一步审核

                tr1.Style["Display"] = "None";

                #endregion 特殊情况，下一步审核人

                string QueueId = "";
                if (Convert.ToDecimal(Session["OnTransactionIdBus"].ToString()) == 0 || Convert.ToDecimal(Session["OnTransactionIdBus"].ToString()) == -1)
                {
                    QueueId = CurrentNodeData.Rows[0]["队列编号"].ToString();
                    Session["NextQueueBus"] = "0";

                    #region 绑定数据

                    DataTable NextNodeData = conBll.GetNextNum(Convert.ToDecimal(CurrentNodeData.Rows[0]["流程编号"].ToString()), Convert.ToDecimal(QueueId));
                    if (NextNodeData != null && NextNodeData.Rows.Count > 0)
                    {
                        Session["NextHandleStateBus"] = NextNodeData.Rows[0]["条件状态"].ToString();

                        lblNextNode.Text = NextNodeData.Rows[0]["节点名称"].ToString();
                    }
                    else
                    {
                        Session["NextHandleStateBus"] = "3";
                        lblNextNode.Text = "归档";
                    }

                    #endregion 绑定数据
                }
                else
                {
                }

                // BindHt(Session["vprocessid"].ToString(), Session["qnum"].ToString());//

                bindGetCirculationConditon(CurrentNodeData.Rows[0]["流程编号"].ToString(), Session["QueueIdBus"].ToString());

                //绑定下一步办理节点
                if (ddlConditions.SelectedValue != null && ddlConditions.SelectedValue != "-1")
                {
                }
            }
            else
            {
                Session["NextHandleStateBus"] = "0";
            }

            #endregion 绑定当前节点信息
        }

        /// <summary>
        /// 绑定转交条件
        /// </summary>
        /// <param name="processId">流程编号</param>
        /// <param name="firstQueue">左节点</param>
        private void bindGetCirculationConditon(string processId, string firstQueue)
        {
            DataTable ConditonData = new Lc_ConditionBLL().GetCirculationConditon(processId, firstQueue, "业务办理", "");

            if (ConditonData == null)
            {
                ConditonData = new DataTable();
                ConditonData.Columns.Add("id");
                ConditonData.Columns.Add("name");
                //  ConditonData.Columns.Add("name");
            }
            if (ConditonData.Rows.Count == 0)
            {
                DataRow dr = ConditonData.NewRow();
                dr["id"] = "-1";
                dr["name"] = "归档";
                ConditonData.Rows.InsertAt(dr, 0);
            }

            ddlConditions.DataSource = ConditonData;
            ddlConditions.DataTextField = "name";
            ddlConditions.DataValueField = "id";
            ddlConditions.DataBind();
            ddlConditionsl_SelectedIndexChanged(null, null);
        }

        /// <summary>
        /// 绑定特殊流转条件
        /// </summary>
        private void BindHt()
        {
            DataTable dt = new Lc_QueueBLL().processQueue(Session["QueneNumber"].ToString(), decimal.Parse(Session["Vbusiness"].ToString()));
            PublicMethod.BindDDL(ddlht, dt, "n_queue_id", "c_node_name", true);
        }

        private void Bindyj()
        {
            DataTable dt = new Sys_OptinionBLL().BindData(lognUser.USER_ID.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                PublicMethod.BindDDL(ddlReplySelect, dt, "C_OPIN_NAME", "C_OPIN_NAME", true);
            }
            else
            {
                DataRow dr = dt.NewRow();
                dr["N_OPIN_ID"] = "-1";
                dr["C_OPIN_NAME"] = "--请选择--";
                dt.Rows.InsertAt(dr, 0);

                DataRow dr2 = dt.NewRow();
                dr2["N_OPIN_ID"] = "0";
                dr2["C_OPIN_NAME"] = "拟同意";
                dt.Rows.InsertAt(dr2, 1);

                DataRow dr3 = dt.NewRow();
                dr3["N_OPIN_ID"] = "1";
                dr3["C_OPIN_NAME"] = "不同意";
                dt.Rows.InsertAt(dr3, 2);

                ddlReplySelect.DataSource = dt;
                ddlReplySelect.DataTextField = "C_OPIN_NAME";
                ddlReplySelect.DataValueField = "C_OPIN_NAME";
                ddlReplySelect.DataBind();
            }

            ddlReplySelect.SelectedIndex = 0;
        }

        ///// <summary>
        ///// 转换条件绑定lable值
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        protected void radbllx_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radbllx.SelectedValue == "0")
            {
                tr1.Style["Display"] = "None";

                ddlConditionsl_SelectedIndexChanged(sender, e);
            }
            else
            {
                tr1.Style["Display"] = "table-row";

                ddlht_SelectedIndexChanged(sender, e);
            }
        }

        protected void ddlConditionsl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlConditions.SelectedValue != null)
            {
                //正常流转
                if (ddlConditions.SelectedValue != "-1" && radbllx.SelectedValue == "0")
                {
                    Lc_Condition conModel = conBll.GetInfoById(ddlConditions.SelectedValue);

                    Lc_Queue qModel = new Lc_QueueBLL().GetInfoById(conModel.N_QUEUE_ID_RIGHT.ToString());

                    lblNextNode.Text = new Lc_NodeBLL().GetInfoById(qModel.N_NODE_ID.ToString()).C_NODE_NAME;

                    #region 检查下一步是否选择人员

                    decimal autyid = qModel.N_AUDIT_TYPE;
                    //审核方式(0.单人；1.多人审核1人通过及通过；2.多人审核全部通过才通过;3 指定人员办理;4 默认为片区管理员; 5指定特定操作产生的人员)
                    if (qModel.N_TRAN_TYPE == 0)
                    {
                        if (autyid == 3)
                        {
                            //指定人员
                            lblNextBlr.Text = "";
                        }
                        else if (autyid == 5)
                        {
                            //申请人
                            lblNextBlr.Text = new Sys_UserBLL().GetInfoById(Session["BusinessCzyIdBus"].ToString()).USER_NAME;
                        }
                        else
                        {
                            lblNextBlr.Text = new Sys_UserBLL().GetUserNameS(qModel.C_QUEUE_TRANSACTION_ID);
                        }
                    }
                    else
                    {
                        //岗位办理
                    }
                    if (autyid != 3)
                    {
                        tr3.Style["Display"] = "None";
                    }
                    else
                    {
                        tr3.Style["Display"] = "table-row";
                    }

                    #endregion 检查下一步是否选择人员
                }
                else
                {
                    lblNextNode.Text = "归档";
                    lblNextBlr.Text = "";
                }
            }
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            //检查，在同一浏览器同时登陆打开两个页面时，可能出现的错误
            if (Convert.ToDecimal(Session["tranid"].ToString()) != Convert.ToDecimal(Session["TransactionIdBus"].ToString()))
            {
                MessageBox.ShowAndRedirect(this.Page, "当前节点已被登陆！请退出办理窗口！", "../../Right.aspx");
            }

            if (radbllx.SelectedValue == "1")
            {
                if (ddlht.SelectedValue == "-1")
                {
                    MessageBox.Show(Page, "特殊流转，请选择流转条件！");
                    return;
                }
            }

            int type = 1;
            Lc_Transaction lcModel = new Lc_Transaction();

            lcModel = tranBll.GetInfoById(Session["tranid"].ToString());

            lcModel.C_TRANSACTION_COMMENT = txt_TransactAttitude.Text.Trim();
            lcModel.N_TRANSACTION_STATE = 0;

            if (radbllx.SelectedValue == "1")
            {
                type = 2;
                if (radtz.SelectedValue == "0")
                {
                    lcModel.N_TRANSACTION_STATE = 3;
                }
                else
                {
                    lcModel.N_TRANSACTION_STATE = 4;
                }
            }

            lcModel.C_TRANSACTOR_SIGNATURE = "0";

            string nexConid = ddlConditions.SelectedValue;

            string zduid = hidsx.Value;

            string n_jumpnode = ddlht.SelectedValue;
            //如果流程为子流程直接中转
            /*if (lcModel.N_QUEUE_ID==)
            {
            }*/
            string rest = tranBll.PassTran(lcModel, lognUser.USER_ID.ToString(), nexConid, zduid, n_jumpnode, type);

            if (rest == "1")
            {
                if (lcModel.N_QUEUE_ID == 212 || lcModel.N_QUEUE_ID == 214 || lcModel.N_QUEUE_ID == 216)
                {
                    //执行关闭当前并行流程
                    var sql = @"UPDATE LC_TRANSACTION T1 SET T1.N_TRANSACTION_STATE = 1 WHERE T1.N_BUSINESS_ID={0} AND T1.N_QUEUE_ID IN(211,213,215)";
                    sql = string.Format(sql, lcModel.N_business_id);
                    tranBll.NoQuery(sql);
                }
                MessageBox.ShowAndRedirect(Page, "【系统提示】办理成功", "../../Right.aspx");
            }
            else
            {
                MessageBox.Show(Page, "办理失败！");
            }
        }

        protected void btnzx_Click(object sender, EventArgs e)
        {
            //检查，在同一浏览器同时登陆打开两个页面时，可能出现的错误
            if (Convert.ToDecimal(Session["tranid"].ToString()) != Convert.ToDecimal(Session["TransactionIdBus"].ToString()))
            {
                MessageBox.ShowAndRedirect(this.Page, "当前节点已被登陆！请退出办理窗口！", "../../Right.aspx");
            }

            int type = 3;
            Lc_Transaction lcModel = new Lc_Transaction();

            lcModel = tranBll.GetInfoById(Session["tranid"].ToString());

            lcModel.C_TRANSACTION_COMMENT = txt_TransactAttitude.Text.Trim();
            lcModel.N_TRANSACTION_STATE = 0;

            lcModel.C_TRANSACTOR_SIGNATURE = "0";

            string nexConid = ddlConditions.SelectedValue;

            string zduid = hidsx.Value;

            string n_jumpnode = ddlht.SelectedValue;

            string rest = tranBll.PassTran(lcModel, lognUser.USER_ID.ToString(), nexConid, zduid, n_jumpnode, type);

            if (rest == "1")
            {
                MessageBox.ShowAndRedirect(Page, "【系统提示】注销成功", "../../Right.aspx");
            }
            else
            {
                MessageBox.Show(Page, "注销失败！");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
        }

        protected void ddlht_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlht.SelectedValue != null)
            {
                if (ddlht.SelectedValue != "-1" && radbllx.SelectedValue == "1")
                {
                    //  Lc_Condition conModel = conBll.GetInfoById(ddlht.SelectedValue);

                    Lc_Queue qModel = new Lc_QueueBLL().GetInfoById(ddlht.SelectedValue);

                    lblNextNode.Text = new Lc_NodeBLL().GetInfoById(qModel.N_NODE_ID.ToString()).C_NODE_NAME;

                    #region 检查下一步是否选择人员

                    decimal autyid = qModel.N_AUDIT_TYPE;
                    //审核方式(0.单人；1.多人审核1人通过及通过；2.多人审核全部通过才通过;3 指定人员办理;4 默认为片区管理员; 5指定特定操作产生的人员)
                    if (qModel.N_TRAN_TYPE == 0)
                    {
                        if (autyid == 3)
                        {
                            //指定人员
                            lblNextBlr.Text = "";
                        }
                        else if (autyid == 5)
                        {
                            //申请人
                            lblNextBlr.Text = new Sys_UserBLL().GetInfoById(Session["BusinessCzyIdBus"].ToString()).USER_NAME;
                        }
                        else
                        {
                            lblNextBlr.Text = new Sys_UserBLL().GetUserNameS(qModel.C_QUEUE_TRANSACTION_ID);
                        }
                    }
                    else
                    {
                        //岗位办理
                    }

                    if (autyid != 3)
                    {
                        tr3.Style["Display"] = "None";
                    }
                    else
                    {
                        tr3.Style["Display"] = "table-row";
                    }

                    #endregion 检查下一步是否选择人员
                }
            }
        }
    }
}